package com.jzh.cmhSystem.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jzh.cmhSystem.entity.UserAndDetail;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.List;

@Mapper
@Repository
public interface UserAndDetailMapper extends BaseMapper<UserAndDetail> {


    @Select({
            "<script>",
            "select * ,en.name as enterpriseName,en.contact as contact,en.id as enterpriseId",
            "from t_user_detail ud",
            "JOIN t_user us ON us.id = ud.user_id",
            "JOIN t_enterprise en ON en.admin_id = us.id",
            "<where>",
            "user_id = #{UserId}",
            "</where>",
            "</script>"
    })
    List<UserAndDetail> selectEnUserByUserId(int UserId);

    @Select({
        /*    "<script>",
            "select * ",
            "from t_user_detail ud",
            "JOIN t_user us ON us.id = ud.user_id",
            "<where>",
            "ud.user_id = #{UserId}",
            "</where>",
            "</script>"*/
            "<script>",
            "select * ,se.name as sectionName,en.name as enterpriseName,en.contact as contact,en.id as enterpriseId",
            "from t_user_detail ud",
            "LEFT JOIN t_user us ON us.id = ud.user_id",
            "LEFT JOIN t_section se ON se.id = ud.section_id",
            "LEFT JOIN t_enterprise en ON en.id = se.enterprise_id",
            "<where>",
            "ud.user_id = #{UserId}",
            "</where>",
            "</script>"
    })
    List<UserAndDetail> selectUserById(@Param("UserId") int UserId);


    @Update({
            "<script>",
            "UPDATE t_user_detail",
            "SET nickname=#{nickname}, sex=#{sex}, email=#{email},phone=#{phone}",
            "<where>",
            "user_id = #{ userId}",
            "</where>",
            "</script>"
    })
    void updateUserDetailByUserId(UserAndDetail userAndDetail);

    //    String nickname, String phone, String name, String role
/*    @Select({
            "<script>",
            "select * ,se.name as sectionName,en.name as enterpriseName,en.contact as contact,en.id as enterpriseId",
            "from t_user_detail ud",
            "JOIN t_user us ON us.id = ud.user_id",
            "JOIN t_section se ON se.id = ud.section_id",
            "JOIN t_enterprise en ON en.id = se.enterprise_id",
            "<where>",
            "<if test='nickname != null and nickname.trim() != \"\"'>",
            "  ud.nickname LIKE CONCAT('%', #{nickname}, '%')",
            "</if>",
            "<if test='phone != null and phone.trim() != \"\"'>",
            "AND ud.phone LIKE CONCAT('%', #{phone}, '%')",
            "</if>",
            "<if test='name != null and name.trim() != \"\"'>",
            "AND ud.name LIKE CONCAT('%', #{name}, '%')",
            "</if>",
            "<if test='enterpriseId != null'>",
            "AND en.id = #{enterpriseId}",
            "</if>",
//           "AND us.role = #{role} or us.role = 1",
            "AND us.role = 2 or us.role = 1",
            "</where>",
            "</script>"
    })
    List<UserAndDetail> selectUserList(UserAndDetail userAndDetail);*/
    @Select({
            "<script>",
            "select ud.*, us.*, se.name as sectionName, en.name as enterpriseName, en.contact as contact, en.id as enterpriseId",
            "from t_user_detail ud",
            "LEFT JOIN t_user us ON us.id = ud.user_id",
            "LEFT JOIN t_section se ON se.id = ud.section_id",
            "LEFT JOIN t_enterprise en ON en.id = ud.enterprise_id",
            "<where>",
            "<trim prefixOverrides=\"AND\">",
            "<if test='nickname != null and nickname.trim() != \"\"'>",
            "  ud.nickname LIKE CONCAT('%', #{nickname}, '%')",
            "</if>",
            "<if test='phone != null and phone.trim() != \"\"'>",
            "AND ud.phone LIKE CONCAT('%', #{phone}, '%')",
            "</if>",
            "<if test='name != null and name.trim() != \"\"'>",
            "AND ud.name LIKE CONCAT('%', #{name}, '%')",
            "</if>",
            "<if test='enterpriseId != null'>",
            "AND en.id = #{enterpriseId}",
            "</if>",
            "AND (us.role = 2 OR us.role = 1)",  // 将此条件移动到 trim 块中
            "</trim>",
            "</where>",
            "</script>"
    })
    List<UserAndDetail> selectUserList(UserAndDetail userAndDetail);





    @Select({
            "<script>",
            "select * ,en.name as enterpriseName,en.contact as contact,en.id as enterpriseId",
            "from t_user_detail ud",
            "LEFT JOIN t_user us ON us.id = ud.user_id",
            "LEFT JOIN t_enterprise en ON en.id = ud.enterprise_id",
            "<where>",
            "<trim prefixOverrides=\"AND\">",
            "<if test='nickname != null and nickname.trim() != \"\"'>",
            "  ud.nickname LIKE CONCAT('%', #{nickname}, '%')",
            "</if>",
            "<if test='phone != null and phone.trim() != \"\"'>",
            "AND ud.phone LIKE CONCAT('%', #{phone}, '%')",
            "</if>",
            "<if test='name != null and name.trim() != \"\"'>",
            "AND ud.name LIKE CONCAT('%', #{name}, '%')",
            "</if>",
            "<if test='enterpriseId != null'>",
            "AND ud.id = #{enterpriseId}",
            "</if>",
            "AND us.role = #{role}",
            "</trim>",
            "</where>",
            "</script>"
    })
    List<UserAndDetail> selectEnUserList(UserAndDetail userAndDetail);

}
